#include <iostream>

#include <pthread.h>
#include <unistd.h>



using namespace std;

void* threadfun(void* args)
{
    pthread_detach(pthread_self()); //线程自己可以分离自己
    //但是如果主线程在从线程分离自己前调用join，那么在join执行时判断进程属性还没有分离就不会报错，反而继续等待
    const char* s = static_cast<const char*>(args);
    for(int i = 0;i<5;++i)
    {
        cout<< s <<endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t t;
    pthread_create(&t,nullptr,threadfun,(void*)("thread-run"));
    //pthread_detach(t); //线程分离后不能等待

    sleep(2); //等待从线程分离后再等待

    int n = pthread_join(t,nullptr);
    if(0 != n)
    {
        cerr<<"join error"<<endl;
        exit(-1);
    }

    return 0;
}
